package com.object.module.lq.sys.controller;


import com.object.module.lq.sys.service.TTableService;
import com.object.lq.utile.Constructor;
import com.object.lq.utile.VueConstructor;
import com.object.utils.Q;
import com.object.utils.SpringBootFlushed;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.Map;

@Controller
@RequestMapping("/table")
public class TTableController
{

    @Resource
    private TTableService tTableService;

    @GetMapping("list")
    @ResponseBody
    public Q list(Map<String, Object> map)
    {
        return tTableService.list(map);
    }

    @GetMapping("bySelect")
    @ResponseBody
    public Q bySelect(String tableName)
    {
        return tTableService.byTableNameSelect(tableName);
    }
   @GetMapping("saveSql")
    @ResponseBody
    public Q saveSql(String tableName,String sql)
    {
        return tTableService.saveSql(tableName,sql);
    }



    @Resource
    private Constructor constructor;

    @GetMapping("createCode")
    @ResponseBody
    public Q createCode(@RequestParam(name = "tableName") String tableName, @RequestParam(name = "moduleName") String moduleName)
    {
        try {
            constructor.generate(tableName, moduleName);
        } catch (Exception e) {
            return Q.no(1, "生成错误！！！");
        }
        SpringBootFlushed.flushed();
        return Q.ok().message("后端代码生成成功！");
    }

    @Resource
    private VueConstructor vueConstructor;

    @GetMapping("vueCode")
    public void vueCode(HttpServletResponse response, @RequestParam(name = "tableName") String tableName,
                        @RequestParam(name = "requestUrl") String requestUrl,
                        @RequestParam(name = "moduleName") String moduleName,
                        @RequestParam(name = "path") String path
                        ) throws SQLException
    {
        vueConstructor.contextLoads(response, tableName, moduleName, requestUrl,path);
    }

}
